const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
};

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: '',
    name: 'Super Admin'
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: '',
    name: 'Normal Editor'
  }
};

module.exports = [
  // user login
  {
    url: '/vue-admin-template/user/login',
    type: 'post',
    response: config => {
      const { userNameOrEmailAddress, password } = config.body;
      const token = userNameOrEmailAddress === '13355667788' ? tokens['admin'] : userNameOrEmailAddress === '13311223344' ? tokens['editor'] : '';

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: 'Account and password are incorrect.'
        };
      }
      if (password === '222222') {
        return {
          code: 60201,
          message: '密码错误.'
        };
      }
      return {
        code: 20000,
        data: token
      };
    }
  },

  // get user info
  {
    url: '/vue-admin-template/user/info.*',
    type: 'get',
    response: config => {
      const { token } = config.query;
      const info = users['admin-token'];

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        };
      }

      return {
        code: 20000,
        data: info
      };
    }
  },

  // user logout
  {
    url: '/vue-admin-template/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      };
    }
  },
  // 获取路由
  {
    url: '/vue-admin-template/user/router',
    type: 'get',
    response: config => {
      const router = ['Dashboard', 'Organization', 'Roles', 'LogCenterIndex'];
      return {
        code: 20000,
        data: router
      };
    }
  },
  {
    url: '/vue-admin-template/user/permissions',
    type: 'get',
    response: config => {
      const router = [{
        'name': 'AbpIdentity.Roles',
        'displayName': '角色管理',
        'parentName': null,
        'isGranted': true,
        'allowedProviders': [],
        'grantedProviders': [{
          'providerName': 'R',
          'providerKey': 'admin'
        }],
        'permissions': [{
          'name': 'AbpIdentity.Roles',
          'displayName': '角色管理',
          'parentName': null,
          'isGranted': true,
          'allowedProviders': [],
          'grantedProviders': [
            {
              'providerName': 'R',
              'providerKey': 'admin'
            }
          ]
        }]
      }
      ];
      return {
        code: 20000,
        data: router
      };
    }
  }
];
